package com.haofengsoft.scce.adapter.information;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.ui.consultation.ConsultationDetailActivity;

public class FocusNewAdapter extends BaseAdapter {

    private Context context;
    private List<FocusNewModel> focusNewModelList;
    private LayoutInflater inflater = null;

    public FocusNewAdapter(Context context, List<FocusNewModel> focusNewModelList) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.focusNewModelList = focusNewModelList;

    }

    public List<FocusNewModel> getFocusNewModelList() {
        return focusNewModelList;
    }

    public void setFocusNewModelList(List<FocusNewModel> focusNewModelList) {
        this.focusNewModelList = focusNewModelList;
    }

    @Override
    public int getCount() {
        return focusNewModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return focusNewModelList.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    public void addFocusNewList(List<FocusNewModel> focusNewModelList) {
        this.focusNewModelList.addAll(focusNewModelList);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder = null;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = inflater.inflate(R.layout.consultation_focus_new_item, null);
            viewholder.titleTv = (TextView) convertView.findViewById(R.id.focus_new_item_title_tv);
            viewholder.detailTv = (TextView) convertView.findViewById(R.id.focus_new_item_detail_tv);
            viewholder.timeTv = (TextView) convertView.findViewById(R.id.focus_new_item_time_tv);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        FocusNewModel focusNewModel = (FocusNewModel) getItem(position);
        viewholder.titleTv.setText(focusNewModel.getTitle());
        viewholder.detailTv.setText(focusNewModel.getDetail());
        viewholder.timeTv.setText(focusNewModel.getTime());
        convertView.setOnClickListener(new ItemClickListener(focusNewModel));
        return convertView;
    }

    class ItemClickListener implements OnClickListener {

        FocusNewModel focusNewModel;

        public ItemClickListener(FocusNewModel focusNewModel) {
            this.focusNewModel = focusNewModel;
        }

        @Override
        public void onClick(View view) {
            Intent consultationDetailIntent = new Intent(context, ConsultationDetailActivity.class);
            String sourceUrl = ConstantConfig.API_URL + ConstantConfig.INFO_CONNECT_WEB_URL + focusNewModel.getId();
            consultationDetailIntent.putExtra("sourceUrl", sourceUrl);
            consultationDetailIntent.putExtra("title", focusNewModel.getTitle());
            consultationDetailIntent.putExtra("time", focusNewModel.getTime());
            consultationDetailIntent.putExtra("source", focusNewModel.getSoure());
            consultationDetailIntent.putExtra("newsId", focusNewModel.getId());
            consultationDetailIntent.putExtra("general", focusNewModel.getDetail());
            context.startActivity(consultationDetailIntent);
        }

    }

    class ViewHolder {
        TextView titleTv;
        TextView detailTv;
        TextView timeTv;
    }
}
